package com.iflytek.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.gson.Gson;
import com.iflytek.domain.Hometype;
import com.iflytek.domain.PageBean;
import com.iflytek.domain.Project;
import com.iflytek.domain.ProjectType;
import com.iflytek.service.ProjectService;

@Controller
public class ProjectController {

	@Autowired
	private ProjectService ps;
	
	// 获取导航栏的项目名称
	@RequestMapping(value = "/project/getProjectName.action")
	public void getProjectName(HttpServletResponse response)throws IOException {
		List<Project> projectList = ps.getAllProject();
		Gson gson = new Gson();
		String json = gson.toJson(projectList);
		response.getWriter().write(json);
	}
	
	// 通过pid获取项目下的分类并跳转到项目分类页面
	@RequestMapping(value = "/project/getProjectCategory.action")
	public String getProjectCategory(String pid,String currentPage,Model model){
		String currentPageStr=currentPage;
		if (currentPageStr == null) {
			currentPageStr = "1";
		}
		int currentPage1 = Integer.parseInt(currentPageStr);
		ProjectType projectType = ps.getProjectTypeByProjectId(pid);
		int currentCount = projectType.getPt_currentCount();
		String pt_id=projectType.getPt_id();
		PageBean pageBean = ps.getPageProjectByPid(pid, currentPage1, currentCount,pt_id);
		model.addAttribute("pageBean", pageBean);
		model.addAttribute("pid", pid);
		String url=projectType.getPt_url();
		System.out.println("urlurlurlurlurl    "+url);
		//request.getRequestDispatcher(url).forward(request, response);
		return url;
	}
	
	// 通过Pid跳转到项目下的产品详情页面
	@RequestMapping(value = "/project/intoHometypeInfo.action")
	public String intoHometypeInfo(String htid,Model model){
		Hometype hometype = ps.getHometypeById(htid);
		model.addAttribute("hometype", hometype);
		String projectId=hometype.getProject_id();
		Project project = ps.getProjectByPid(projectId);
		String url=project.getProject_url();
		//request.getRequestDispatcher(url).forward(request, response);
		return url;
	}
	
	//自动补全导航栏
	@RequestMapping(value = "/project/autoSearchWord.action")
	public void autoSearchWord(String search, HttpServletResponse response)throws IOException {
		List<String> searchList=ps.getAutoSearchWord(search);
		Gson gson = new Gson();
		String json = gson.toJson(searchList);
		response.getWriter().write(json);
	}
}
